/**
 * 
 */
package com.dianwoba.core.jackson;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.aspectj.weaver.NewConstructorTypeMunger;

import com.dianwoba.utils.DateUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

/**
 * @author Zhu
 * @date 2015-12-18 下午11:43:47
 * @description
 */
public class DateSerialize extends JsonSerializer<Date> {

	private final DateFormat format = new SimpleDateFormat("HH:mm");

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fasterxml.jackson.databind.JsonSerializer#serialize(java.lang.Object,
	 * com.fasterxml.jackson.core.JsonGenerator,
	 * com.fasterxml.jackson.databind.SerializerProvider)
	 */
	@Override
	public void serialize(Date value, JsonGenerator gen,
			SerializerProvider serializers) throws IOException,
			JsonProcessingException {
		if (value == null) {
			serializers.defaultSerializeValue(value, gen);
			return;
		}
		Date todayStart = DateUtil.getCurrentDate();
		Date yesterdayStart = DateUtil
				.add(todayStart, Calendar.DAY_OF_YEAR, -1);
		if (value.after(todayStart)) {
			gen.writeString("今天 " + format.format(value));
		} else if (value.after(yesterdayStart)) {
			gen.writeString("昨天 " + format.format(value));
		} else {
			serializers.defaultSerializeValue(value, gen);
		}
	}

}
